package org.concordia.wsdl.helper;

import java.util.LinkedList;
import java.util.List;

import org.concordia.wsdl.filter.Filter;
import org.concordia.wsdl.filter.TranditionalFilter;
import org.concordia.wsdl.model.WebService;

public abstract class CompositionUtils {

	
	public static List<WebService> getCandidates(List<WebService> remainingWSList, 
			WebService n, List<String> knownParams){
		
		List<WebService> candidates = new LinkedList<WebService>();
		List<String> inputParams = ListUtils.union(knownParams, n.getOutputs());
		Filter filter = new TranditionalFilter();
	
		List<WebService> remainingWSListCopy = ListUtils.copyListWS(remainingWSList);
		remainingWSListCopy.remove(n);
		
		for(WebService ws : remainingWSListCopy){
			List<String> desiredParams = ws.getInputs();	
			if(filter.check(desiredParams, inputParams)){
				candidates.add(ws);
//				System.out.println("***CAN INVOKE " + ws.getName());
//				for(String s : desiredParams){
//					System.out.println(s);
//				}					
			}else{
// 				System.out.println("***KNOWN");
//				for(String s : inputParams){
//					System.out.println(s);
//				}
//				System.out.println("***CANNOT INVOKE " + ws.getName());
//				for(String s : desiredParams){
//					System.out.println(s);
//				}				
 
			}
		}
		
		return candidates;

	}
	
	public static WebService getBestWS(List<WebService> list){

		WebService best = null;

		if(list.size() > 0){
			best = list.get(0);
		}
		
		for(WebService ws : list){
			if(ws.getScore() < best.getScore()){
				System.out.println("[" + best.getScore() + "->" + ws.getScore() + "]");
				best = ws;
			}
		}
		
		return best;
		
	}
	
}
